/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package centerberg.engine;

import java.awt.Image;
import java.awt.Toolkit;
import java.util.HashMap;
import java.util.Map;


/**
 * Loads the given imagefile and creates a texture based upon it.
 * Supports three different kinds of textures, scaled, tiled and normal.
 * 
 * @author Andreas Svanberg
 */
public class TextureLoader
{
    private Map<String, Texture> loadedTextures = new HashMap<String, Texture>();
    
    TextureLoader()
    {
        
    }
    
    /**
     * Returns the texture with the specified name.
     * 
     * @param name the name of the texture
     * @return     the texture with the specidief name
     */
    public Texture getTexture(String name)
    {
        return loadedTextures.get(name);
    }
    
    /**
     * Attempts to load the texture from the given filename. The name must be
     * unique otherwise the previous texture with the given name will be
     * overwritten.
     * 
     * @param name     the name to give the texture
     * @param filename the file to read the image from
     */
    public void loadTexture(String name, String filename)
    {
        loadTexture(name, filename, Texture.Type.NORMAL, 0, 0);
    }
    
    /**
     * Attempts to load the texture from the given filename. The name must be
     * unique otherwise the previous texture with the given name will be
     * overwritten. The type parameter specifies what type of texture it is and
     * w and h specifies the size.
     * 
     * @param name     the name to give the texture
     * @param filename the file to read the image from
     * @param type     the type of texture
     * @param w        the width of the texture
     * @param h        the height of the texture
     * @see Texture.Type
     */
    public void loadTexture(String name, String filename, Texture.Type type, int w, int h)
    {
        try
        {
            Image img = Toolkit.getDefaultToolkit().createImage(filename);
            Texture texture;
            
            if (type == Texture.Type.SCALED)
                texture = new Texture(img.getScaledInstance(w, h, Image.SCALE_SMOOTH));
            else if (type == Texture.Type.TILED)
                texture = new TiledTexture(img, w, h);
            else
                texture = new Texture(img);
            
            loadedTextures.put(name, texture);
        }
        catch (Exception e) { }
    }
}
